package com.lienlq.voicemusic.ui;

import java.net.URI;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xmlrpc.android.XMLRPCClient;
import org.xmlrpc.android.XMLRPCException;

import com.lienlq.voicemusic.entity.Genre;
import com.lienlq.voicemusic.ui.ArtistFragment.ViewHolder;
import com.lienlq.voicemusic.xmlprc.XMLRPCMethod;
import com.lienlq.voicemusic.xmlprc.XMLRPCMethodCallback;
import com.lienlq.voicemusic.R;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class GenreFragment extends Fragment {
	private ListView listview = null;
	private ArrayList<Genre> genreList = new ArrayList<Genre>();
	JSONObject jsObject;
	Object result;
	JSONArray jsArray;
	Context context; 
	ArrayAdapter<Genre> adapter;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		XMLRPCMethod method=new XMLRPCMethod("selectAllGenre", new XMLRPCMethodCallback() {
			
				@Override
				public void CallFinished(Object result) {
				// TODO Auto-generated method stub
					try{
						jsObject=new JSONObject(result.toString());
						jsArray=jsObject.getJSONArray("genre");
						for(int i=0;i<jsArray.length();i++)
						{
							JSONObject obj=jsArray.getJSONObject(i);
							Genre genre=new Genre(obj.getString("name"));
							genreList.add(genre);
						}
						adapter=new GenreAdapter();
						listview.setAdapter(adapter);
					}catch(Exception e)
					{
						
					}
				}
			});
		Object[] params = null;
		method.call(params);
		
	}


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View v = inflater.inflate(R.layout.fragment_genre, container, false);
        listview = (ListView)v.findViewById(R.id.genres_list_fragment_genre);
        context=getActivity();
		return v;
		
	}


	@Override
	public void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);
	}
	
	class GenreAdapter extends ArrayAdapter<Genre>
	{
		public GenreAdapter()
		{
			super(context, R.layout.item_genre_fragment_genre,genreList);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			View row=convertView;
			ViewHolder holder=null;
			if(row==null)
			{
				LayoutInflater inflater=getActivity().getLayoutInflater();
				row=inflater.inflate(R.layout.item_genre_fragment_genre,parent,false);
				holder=new ViewHolder(row);
				row.setTag(holder);
			}
			else
			{
				holder=(ViewHolder)row.getTag();
			}
			holder.populateFrom(genreList.get(position));
			return row;
			
		}
		
	}
	class ViewHolder
	{
		TextView tvGenreName;
		public ViewHolder(View row)
		{
			tvGenreName=(TextView)row.findViewById(R.id.item_genre);
		}
		 void populateFrom(Genre genre)
		{
			tvGenreName.setText(genre.getName());
		}
	}
}
